package com.zmn.plat.admin.controller.supplier;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.plat.model.entity.supplier.ScmSupplier;
import com.zmn.plat.model.entity.supplier.SupplierCompanyMap;

/**
 * @author DaiZhichao
 * @version v1.0
 * @since 2018/10/26 19:36 类说明：ScmSupplierListController
 */

@Controller
@RequestMapping("/supplier")
public class ScmSupplierListController extends ScmSupplierBaseController {
    private Logger logger = LoggerFactory.getLogger(ScmSupplierBaseController.class);

    /**
     * 描述: [跳转] 供应商列表
     *
     * @author DaiZhichao
     * @since 2018/11/6 21:05
     */
    @RequestMapping("/list")
    @RequiresPermissions("/supplier/list.action")
    public String supplierList(Model model) {

        model.addAttribute("platformCompanySimpleList", getPlatformCompanySimpleList());
        model.addAttribute("statuses", GlobalDict.getStatusList(false));
        return "supplier/list";
    }

    /**
     * 描述: [跳转] 供应商详情
     *
     * @author DaiZhichao
     * @since 2018/11/6 21:05
     */
    @RequestMapping("/addEdit")
    @RequiresPermissions("/supplier/addEdit.action")
    public String supplierDetail(@RequestParam(required = false, defaultValue = "0") Integer supplierId, Model model) {

        ScmSupplier supplier = supplierId.equals(0) ? new ScmSupplier() {
            {
                setStatus((short)GlobalDict.STATUS_ENABLE);
                setZmnCompanyId(1);
            }
        } : scmSupplierService.findByKey(supplierId);

        // 可以使用该供应商的公司列表
        List<Integer> companyMapList = supplierCompanyMapService.listBySupplierId(supplierId).stream().map(SupplierCompanyMap::getCompanyId)
            .collect(Collectors.toList());

        model.addAttribute("companyMapList", companyMapList);
        model.addAttribute("platformCompanySimpleList", getPlatformCompanySimpleList());
        model.addAttribute("companySimpleList", getSupplierCompanySimpleList());
        model.addAttribute("supplier", supplier);
        model.addAttribute("statuses", GlobalDict.getStatusList(false));
        return "supplier/addedit";
    }

}
